/** 
* 获取本周、本季度、本月、上月的开端日期、停止日期 
*/
var now = new Date(); //当前日期 
var nowDayOfWeek = now.getDay(); //今天本周的第几天 
var nowDay = now.getDate(); //当前日 
var nowMonth = now.getMonth(); //当前月 
var nowYear = now.getYear(); //当前年 
nowYear += (nowYear < 2000) ? 1900 : 0; // 

var lastMonthDate = new Date(); //上月日期 
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();

//获取今天的日期
function getToDayDate(type) {
  return formatDateCommon(now, type);
}

//获取昨天的提起
function getYesterdayDate(type) {
  var yesterdayDate = new Date(nowYear, nowMonth, nowDay - 1);
  return formatDateCommon(yesterdayDate, type);
}

//获得本周的开端日期 
function getWeekStartDate(type) {
  var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  return formatDateCommon(weekStartDate, type);
}

//获得本周的停止日期 
function getWeekEndDate(type) {
  var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  return formatDateCommon(weekEndDate, type);
}

//获取上周的开端日期
function getLastWeekStartDate(type) {
  var lastWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
  return formatDateCommon(lastWeekStartDate, type);
}

//获取上周的停止日期
function getLastWeekEndDate(type) {
  var lastWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) - 7);
  return formatDateCommon(lastWeekEndDate, type);
}

//获得本月的开端日期 
function getMonthStartDate(type) {
  var monthStartDate = new Date(nowYear, nowMonth, 1);
  return formatDateCommon(monthStartDate, type);
}

//获得本月的停止日期 
function getMonthEndDate(type) {
  var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  return formatDateCommon(monthEndDate, type);
}

//获得上月开端时候 
function getLastMonthStartDate(type) {
  var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  return formatDateCommon(lastMonthStartDate, type);
}

//获得上月停止时候 
function getLastMonthEndDate(type) {
  var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
  return formatDateCommon(lastMonthEndDate, type);
}

//获得本季度的开端日期 
function getQuarterStartDate(type) {
  var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  return formatDateCommon(quarterStartDate, type);
}

//或的本季度的停止日期 
function getQuarterEndDate(type) {
  var quarterEndMonth = getQuarterStartMonth() + 2;
  var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
  return formatDateCommon(quarterStartDate, type);
}

//获得某月的天数 
function getMonthDays(myMonth) {
  var monthStartDate = new Date(nowYear, myMonth, 1);
  var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  return days;
}

//获得本季度的开端月份 
function getQuarterStartMonth() {
  var quarterStartMonth = 0;
  if (nowMonth < 3) {
    quarterStartMonth = 0;
  }
  if (2 < nowMonth && nowMonth < 6) {
    quarterStartMonth = 3;
  }
  if (5 < nowMonth && nowMonth < 9) {
    quarterStartMonth = 6;
  }
  if (nowMonth > 8) {
    quarterStartMonth = 9;
  }
  return quarterStartMonth;
}

function formatDateCommon(date, type) {
  if (type == 'date')
    return formatDate(date);
  else if (type == 'datetime')
    return formatDateLong(date);
  else if (type == 'month')
    return formatMonth(date);
}

//格式化日期：yyyy-MM
function formatMonth(date) {
  var myyear = date.getFullYear();
  var mymonth = date.getMonth() + 1;
  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  return (myyear + "-" + mymonth);
}

//格式化日期：yyyy-MM-dd 
function formatDate(date) {
  var myyear = date.getFullYear();
  var mymonth = date.getMonth() + 1;
  var myweekday = date.getDate();

  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  return (myyear + "-" + mymonth + "-" + myweekday);
}

//格式化日期：yyyy-MM-dd hh:mm:ss
function formatDateLong(date) {
  var myyear = date.getFullYear();
  var mymonth = date.getMonth() + 1;
  var myweekday = date.getDate();
  var myhour = date.getHours();
  var myminute = date.getMinutes();
  var mysecond = date.getSeconds();

  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  if (myhour < 10) {
    myhour = "0" + myhour;
  }
  if (myminute < 10) {
    myminute = "0" + myminute;
  }
  if (mysecond < 10) {
    mysecond = "0" + mysecond;
  }
  return (myyear + "-" + mymonth + "-" + myweekday + " " + myhour + ":" + myminute + ":" + mysecond);
}

//秒数转换成 0天0小时0分钟0秒
function ConverTimeToDHMS(second_time) {
  var time = parseInt(second_time) + "秒";
  if (parseInt(second_time) > 60) {
    var second = parseInt(second_time) % 60;
    var min = parseInt(second_time / 60);
    time = min + "分钟" + second + "秒";
    if (min > 60) {
      min = parseInt(second_time / 60) % 60;
      var hour = parseInt(parseInt(second_time / 60) / 60);
      time = hour + "小时" + min + "分钟" + second + "秒";
      if (hour > 24) {
        hour = parseInt(parseInt(second_time / 60) / 60) % 24;
        var day = parseInt(parseInt(parseInt(second_time / 60) / 60) / 24);
        time = day + "天" + hour + "小时" + min + "分钟" + second + "秒";
      }
    }
  }
  return time;
}

function StringToDate(DateStr) {
  var temp = DateStr.split('-');
  if (temp.length == 0)
    return now;
  if (temp.length == 1)
    DateStr = DateStr + "-01";
  if (temp.length == 2)
    DateStr = DateStr + "-01";
  var converted = Date.parse(DateStr);
  var myDate = new Date(converted);
  if (isNaN(myDate)) {
    //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';
    var arys = DateStr.split('-');
    myDate = new Date(arys[0], --arys[1], arys.length>=3?arys[2]:"1");
  }
  return myDate;
}

function CompareTime(stime, etime) {
  var start = StringToDate(stime);
  var end = StringToDate(etime);
  if (end < start) {
    return false;
  }
  return true;
}